package com.dahuyou.find.clazz.classpath.entry.impl;

import com.dahuyou.find.clazz.classpath.entry.Entry;

import java.io.IOException;
import java.nio.file.*;

/**
 * jar,zip形式的entry
 */
public class JarOrZipEntry implements Entry {
    private Path absolutePath;

    public JarOrZipEntry(String path) {
        //获取绝对路径
        this.absolutePath = Paths.get(path).toAbsolutePath();
    }

    @Override
    public byte[] readClass(String className) throws IOException {
        try (FileSystem zipFs = FileSystems.newFileSystem(absolutePath, null)) {
            return Files.readAllBytes(zipFs.getPath(className));
        }
    }

    @Override
    public String toString() {
        return this.absolutePath.toString();
    }

}
